package com.example.carprofile;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class ParseXML {
	

	public boolean editXML(String[] itemValues, InputStream XmlFileInputStream) {

		try {
			String content = readTextFile(MainActivity.myXML);
			String[] items = { "carMake", "carModel", "fuelType", "carYear",
					"carColor", "engineHP", "engineCcm" };

			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(content);

			Node cars = doc.getElementsByTagName("cars").item(0);
			Element car = doc.createElement("car");
			Element newItem;

			for (int i = 0; i < items.length; i++) {
				newItem = doc.createElement(items[i]);
				newItem.setTextContent(itemValues[i]);
				car.appendChild(newItem);
			}
			cars.appendChild(car);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(MainActivity.myXML);
			transformer.transform(source, result);
			return true;
		} catch (Exception e) {
			android.util.Log.e("tag", "", e);
			return false;
		}
	}

	@SuppressWarnings("null")
	public String[] giveCars(String cars) {

		try {
			//novi komentar
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(cars);

			String[] carsInFile = null;
			NodeList myCars = doc.getElementsByTagName("car");
			Node myCar;
			NodeList spec;

			for (int i = 0; i < myCars.getLength(); i++) {
				myCar = myCars.item(i);
				spec = myCar.getChildNodes();
				carsInFile[i] = spec.item(0).toString() + " "
						+ spec.item(1).toString();
			}
			return carsInFile;
		} catch (Exception e) {
			android.util.Log.e("tag", "", e);
		}
		return null;
	}

	public String readTextFile(String myXML) {
		try {
			 
			InputStream in = AppContext.getAppContext().openFileInput(myXML);
			if (in != null) {
			InputStreamReader tmp = new InputStreamReader(in); 
			BufferedReader reader = new BufferedReader(tmp);
			 
			String str;
			StringBuilder buf = new StringBuilder();
			 
			while ((str = reader.readLine()) != null) { 
				buf.append(str+"\n");
			}
			 
			in.close();
			return buf.toString();
			} else {
				return null;
			}
			 
		}
		catch (java.io.FileNotFoundException e) {
			return null;
		}
		catch (Throwable t) {
			return null;
		}
	}

}
